package mulegame;

import java.awt.Color;
import mulegame.Player.Race;

/**
 * Sets stores initial amounts of resources and returns amounts that it has and how much they cost. Sets cost of 
 * mules and lets player buy them. Also lets player buy ore, energy, and food based on whether the store has enough. 
 * @author AREA 29
 *
 */
public class Store{
    
    private int food, energy, ore, mules, crystite;
    
    //these are public to allow access from outside the class
    //but final so the values cannot be changed
    public final int costFood = 30;
    public final int costEnergy = 25;
    public final int costSmithore = 50;
    public final int costCrystite = 100;
    public final int costBaseMule = 100;
    
	/**
	 * stores different item types 
	 * 
	 * @author AREA 29
	 */
    public enum ItemType {MULE_FOOD, MULE_ENERGY, MULE_ORE, FOOD, ENERGY, ORE};
    /**
     * sets how much the store has a each resource in the beginning of the game based on game level
     * @param diff difficulty level of game
     */
    public Store(GameConfig.Difficulty diff){
        switch(diff){
        case BEGINNER:
            food = energy = 16;
            crystite = ore = 0;
            mules = 25;
            break;
        case STANDARD:
        case TOURNAMENT:
            food = energy = ore = 8;
            mules = 14;
            crystite = 0;
            break;
        default:
            food = energy = ore = mules = crystite = 0;
        }
    }
    
    /**
     * sets Store resource quantities based on a string that is passed in
     * @param lines a string holding store info 

     */
    public Store(String[] lines){
        Logger.println("making player from definition");
        for(String line : lines){
            String[] args = line.split("\\s:\\s");
            if(args[0].equals("Food Qty")){
                food = Integer.parseInt(args[1]);       
            }
            if(args[0].equals("Energy Qty")){
                energy = Integer.parseInt(args[1]);   
            }
            if(args[0].equals("Ore Qty")){
                ore = Integer.parseInt(args[1]);   
            }
            if(args[0].equals("Mules Qty")){
                mules = Integer.parseInt(args[1]);   
            }
            if(args[0].equals("Crystite Qty")){
                crystite = Integer.parseInt(args[1]);       
            }
        }
    }
    
    /**
     * returns mules 
     * 
     * @return mules
     */
    public int getMules(){
        return mules;   
    }
    
    /**
     * returns energy
     * @return energy
     */
    public int getEnergy(){
        return energy;   
    }
    
    /**
     * returns food
     * @return food
     */
    public int getFood(){
        return food;   
    }
    
    /**
     * returns ore
     * @return ore
     */
    public int getOre(){
        return ore;   
    }
    
    /**
     * returns crystite
     * @return crystite
     */
    public int getCrystite(){
        return crystite;   
    }
    
    /**
     * returns cost of energy
     * @return costEnergy
     */
    public int getEnergyCost(){
        return costEnergy;   
    }
    
    /**
     * returns cost of food
     * @return costFood
     */
    public int getFoodCost(){
        return costFood;   
    }
    
    /**
     * returns cost of ore
     *
     * @return costSmithore
     */
    public int getOreCost(){
        return costSmithore;   
    }
    
    /**
     * returns cost of Crystite
     * @return costCrystite
     */
    public int getCrystiteCost(){
        return costCrystite;   
    }
    
    /**
     * Determines the cost for a mule of the type specified.
     *
     *@param type The mule type in consideration
     */
    public int getMuleCost(Mule type){
        int cost = costBaseMule;
        switch(type){
        case FOOD:
            cost += 25;
            break;
        case ENERGY:
            cost += 50;
            break;
        case ORE:
            cost += 75;
            break;
        default:
            Logger.println("getMuleCost: unknown type type, how did you get here?");
            break;
        }
        return cost;
    }
    
    /**
     * Charges the given player for purchasing a mule. Mule cost is dependent
     * on mule type.
     *
     *@param p The player to charge
     *@param type The type of mule to sell
     */
    public boolean sellMuleTo(Player p, Mule type){
        int price = getMuleCost(type);
        if(p.charge(price)){    // Player's charge method handles the check to 
                                // see if player has enough money                   
            //give the player the requested mule
            p.giveMule(type);
            //remove the mule that was sold
            mules--;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * sells food to player, calculates price and charges player the prince, returns true if player can afford it
     * @param p player
     * @param quantity amount of food being bought
     * @return true
     */
    public boolean sellFoodTo(Player p, int quantity){
        int price = quantity * costFood;
        if(p.charge(price)){
            food -= quantity;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * sells energy to player, calculates price and charges player the prince, returns true if player can afford it
     * @param p player
     * @param quantity amount of energy being bought
     * @return true
     */
    public boolean sellEnergyTo(Player p, int quantity){
        int price = quantity * costEnergy;
        if(p.charge(price)){
            energy -= quantity;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * sells ore to player, calculates price and charges player the prince, returns true if player can afford it
     * @param p player
     * @param quantity amount of ore being bought
     * @return true
     */
    public boolean sellOreTo(Player p, int quantity){
        int price = quantity * costSmithore;
        if(p.charge(price)){
            ore -= quantity;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * If the player has enough food adds food to player and pays for cost
     * @param p player
     * @param quantity amount of food being bought
     * @return ret
     */
    public boolean buyFoodFrom(Player p, int quantity){
        boolean ret = p.takeFood(quantity);
        if(ret){
            food += quantity;   
            p.pay(costFood * quantity);
        }
        return ret;
    }
    
    /**
     * 
     * If the player has enough energy adds energy to player and pays for cost
     * @param p player
     * @param quantity amount of energy being bought
     * @return ret
     */
    public boolean buyEnergyFrom(Player p, int quantity){
        boolean ret = p.takeEnergy(quantity);
        if(ret){
            energy += quantity;
            p.pay(costEnergy * quantity);
        }
        return ret;
    }
    
    /**
     * 
     * If the player has enough ore adds ore to player and pays for cost
     * @param p player
     * @param quantity amount of ore being bought
     * @return ret
     */
    public boolean buyOreFrom(Player p, int quantity){
        boolean ret = p.takeOre(quantity);
        if(ret){
            ore += quantity;   
            p.pay(costSmithore * quantity);
        }
        return ret;
    }
    
    /**
     * Prints out data on Store
     * 
     * @return desc string with data of store resources
     */

     public String toString(){
     	
	     //  What do we need to save
	     //    	 food, energy, ore, mules, crystite;
	     	
	 	 String desc = "Store Information";
	 	 desc += "\n";
         desc += "Food Qty : " + Integer.toString(getFood()) + "\n";
         desc += "Energy Qty : " + Integer.toString(getEnergy()) + "\n";
         desc += "Ore Qty : " + Integer.toString(getOre()) + "\n";
         desc += "Mules Qty : " + Integer.toString(getMules()) + "\n";
         desc += "Crystite Qty : " + Integer.toString(getCrystite()) + "\n";
         
 		return desc;   
 	}
 }

